/************************************************************************/
/*
 * 
 * All rights reserved.
 *  
 *
 * This file is a part of the I Foundation Classes library.
 * Author:		 
 * Create on:	2012-11-6
 */
/************************************************************************/


#include "ByteString.h"

//#define MZSTRING_INIT_SIZE		4

#pragma warning(disable:4996)

int mzwcslen(const char* pstr)
{
	if (pstr==0)
	{
		return 0;
	}
	return strlen(pstr);
}

CByteStringW::CByteStringW(void)
:	m_pData(&m_Zero)
,	m_Zero(0)
,	m_nSize(0)
{
}
CByteStringW::CByteStringW(size_t nSize)
:	m_pData(&m_Zero)
,	m_Zero(0)
,	m_nSize(0)
{
	Alloc(nSize);
}
CByteStringW::CByteStringW(const CByteStringW &Str)
:	m_pData(&m_Zero)
,	m_Zero(0)
,	m_nSize(0)
{
	*this = Str.C_Str();
}
CByteStringW::CByteStringW(const char *pStr)
:	m_pData(&m_Zero)
,	m_Zero(0)
,	m_nSize(0)
{
	*this = pStr;
}

bool CByteStringW::SetBufferSize(size_t nCharCount)
{
	return Alloc(nCharCount)!=0;
}
CByteStringW::~CByteStringW(void)
{
}
void CByteStringW::Reset()
{
	m_Zero = 0;
	if (m_pData!=0 && (m_pData!=&m_Zero))
	{
		LocalFree(m_pData);
	}
	m_pData = &m_Zero;
	m_nSize = 0;
}
void* CByteStringW::Alloc(size_t nCharCount)
{
	Reset();

	m_pData = (char*)LocalAlloc(LPTR, nCharCount*sizeof(char));

	if (m_pData==0)
	{
		Reset();
	}
	else
		m_nSize = nCharCount;

	return m_pData;
}
size_t CByteStringW::GetBufferSize() const
{
	return m_nSize;
}
char* CByteStringW::C_Str() const
{
	return m_pData;
}
void CByteStringW::ZeroBuffer()
{
	if(m_pData!=(&m_Zero) && m_pData!=0 && m_nSize>0)
		memset(m_pData, 0, m_nSize*sizeof(char));
}
size_t CByteStringW::Length() const
{
	if(!m_pData)
		return 0;
	return mzwcslen(m_pData);
}
CByteStringW& CByteStringW::operator=(const CByteStringW &Str)
{
	return *this=Str.C_Str();
}
CByteStringW& CByteStringW::operator=(const char *pStr)
{
	//if(pStr==C_Str())
	//	return *this;
	size_t n = 0;
	n=mzwcslen(pStr);

	if(n==0)
	{
		Reset();
		return *this;
	}

	if ((n + 1) < GetBufferSize())
	{
		ZeroBuffer();
	}
	else
	{
		SetBufferSize(n + 1);
	}

	char* p = C_Str();
	if(p)
		strcpy(p, pStr);
	return *this;
}
/*
CByteStringW::operator LPCTSTR() const
{
	return (LPCTSTR)C_Str();
}
CByteStringW::operator LPTSTR() const
{
	return (LPTSTR)C_Str();
}
*/
CByteStringW CByteStringW::operator+(const CByteStringW &Str)
{
	CByteStringW sTemp;
	int n1 = Length();
	int n2 = Str.Length();
	sTemp.SetBufferSize(n1+n2+1);
	if(C_Str())
		strcpy(sTemp.C_Str(), C_Str());
	char* p = sTemp.C_Str()+n1;
	if(p && Str.C_Str())
		strcpy(p, Str.C_Str());
	return sTemp;
}
CByteStringW CByteStringW::operator+(const char *pStr)
{
	CByteStringW sTemp;
	int n1 = Length();
	int n2 = mzwcslen(pStr);
	sTemp.SetBufferSize(n1+n2+1);
	if(C_Str())
		strcpy(sTemp.C_Str(), C_Str());
	char* p = sTemp.C_Str()+n1;
	if(p && pStr)
		strcpy(p, pStr);
	return sTemp;
}
bool CByteStringW::operator ==(const char *pStr) const
{
	return Compare(pStr)==0;
}
bool CByteStringW::operator ==(const CByteString& _Str) const
{
	return Compare(_Str.C_Str())==0;
}
int CByteStringW::Compare( const char* pStr ) const
{
	if(pStr==NULL)
	{
		if(IsEmpty())
			return 0;
		else
			return 1;
	}
	else
	{
		int nLen = strlen(pStr);
		if (IsEmpty() && nLen>0)
			return -1;
	}

	int nRet = strcmp(C_Str(), pStr);
	return nRet;
}

int CByteStringW::Compare( const CByteString& _Str ) const
{
	return Compare(_Str.C_Str());
}
CByteStringW CByteStringW::SubStr(int nPosStart, int nCount)
{
	CByteStringW sResult;
	int length = Length();
	if (length==0)
		return sResult;
	if (nPosStart>=length || nPosStart<0)
		return sResult;
	if (nCount<=0)
		return sResult;

	sResult.SetBufferSize(nCount+1);
	int nFor = (nPosStart+nCount>length) ? (length-nPosStart) : nCount;
	char* p1 = C_Str() + nPosStart;
	char* p2 = sResult.C_Str();
	for (int n=0; n<nFor; n++)
	{
		*(p2+n) = *(p1+n);
	}
	return sResult;
}

bool CByteStringW::IsEmpty() const
{
	if (Length()==0)
		return true;

	return false;
}
